//
//  WQChoiseRoomListController.m
//  WisdomQuestor
//
//  Created by 纪景城 on 2024/12/24.
//

#import "WQChoiseRoomListController.h"

@interface WQChoiseRoomListController ()

@property (nonatomic, assign) NSInteger           choiseIndex;

@property (nonatomic, strong) UIButton           *cancelButton;
@property (nonatomic, strong) UIButton           *doneButton;

@end

@implementation WQChoiseRoomListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择房间";
    self.tableView.mj_header.hidden = YES;
    
    self.choiseIndex = -1;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.cancelButton];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.doneButton];
}

#pragma mark ------- UITableViewDelegate -------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return WQShare.userHomeModel.roomList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kFragmemtFloat(60);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ImageLabelCell *cell = [ImageLabelCell ff_CellDequeueWithId:tableView];
    [cell choiseRoomMasLayout];
    
    
    ThingSmartRoomModel *roomModel = WQShare.userHomeModel.roomList[indexPath.row];
    
    cell.titleLabel.text = roomModel.name;
    
    
    if (indexPath.row == self.choiseIndex) {
        cell.mainImageView.image = kImageName(@"wq_xuanzhong_sele");
    } else {
        cell.mainImageView.image = kImageName(@"wq_xuanzhong_no");
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.choiseIndex = indexPath.row;
    [self.tableView reloadData];
}

#pragma mark ------- Lazy Methods -------

- (void)fragmentUI_ViewSet {
    
}

- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton initWithTitle:@"取消" titleColor:k_Color_themeColor fontSize:15];

        kWEAKSELF
        [_cancelButton kj_addAction:^(UIButton * _Nonnull kButton) {
            if (weakSelf.choiseRoomTemplateBlock) {
                weakSelf.choiseRoomTemplateBlock(NO, nil);
            }
            [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
        }];
    }
    return _cancelButton;
}

- (UIButton *)doneButton {
    if (!_doneButton) {
        _doneButton = [UIButton initWithTitle:@"确定" titleColor:k_Color_themeColor fontSize:15];

        kWEAKSELF
        [_doneButton kj_addAction:^(UIButton * _Nonnull kButton) {
            
            if (weakSelf.choiseRoomTemplateBlock) {
                
                if (weakSelf.choiseIndex >= 0) {
                    weakSelf.choiseRoomTemplateBlock(YES, WQShare.userHomeModel.roomList[weakSelf.choiseIndex]);
                } else {
                    weakSelf.choiseRoomTemplateBlock(NO, nil);
                }
            }
            [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
        }];
    }
    return _doneButton;
}

@end
